import { ProductApi } from '@/api/product'
import useCategoryStore from '@/store/modules/category'

// 三级分类逻辑层
export const useCategory = () => {
  const categoryStore = useCategoryStore()

  // 获取一级分类
  const getList1 = async () => (categoryStore.c1List = await ProductApi.getCategoryList1())

  // 一级分类变化事件
  const cateOneChange = async (v: number) => {
    categoryStore.c1 = v
    categoryStore.c2 = ''
    categoryStore.c3 = ''
    categoryStore.disabled = true
    categoryStore.c2List = await ProductApi.getCategoryList2(categoryStore.c1)
  }
  // 二级分类分化事件
  const cateTwoChange = async (v: number) => {
    categoryStore.c2 = v
    categoryStore.c3 = ''
    categoryStore.disabled = true
    categoryStore.c3List = await ProductApi.getCategoryList3(categoryStore.c2)
  }
  // 三级分类变化事件
  const cateThreeChange = (v: number) => {
    categoryStore.c3 = v
    categoryStore.disabled = false
  }

  return { categoryStore, getList1, cateOneChange, cateTwoChange, cateThreeChange }
}
